package olegroshka.src.com.bofa.olegroshka.common;

public class PrimitiveLatch {
	private int count;
	private final Object monitor = new Object();

	public PrimitiveLatch(int count) {
		this.count = count;
	}

	public void countDown() {
		synchronized (monitor) {
			if ( count > 0 && (--count) == 0) {
				monitor.notify();
			}
		}
	}

	public void await() throws InterruptedException {
		synchronized (monitor) {
			while (count > 0) {
				wait();
			}
		}
	}
	
	public void await(long timeout) throws InterruptedException {
		synchronized (monitor) {
			while (count > 0) {
				wait(timeout);
			}
		}
	}
	
	public boolean ready() {
        synchronized (monitor) {
    		return count == 0;
		}
	}
}